package com.example.teaknow.Dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.teaknow.Bean.NewsBean;
import com.example.teaknow.Util.DBHelper;

import java.util.ArrayList;


/**
 * @Author: Stars
 */
public class NewsDao {
    private DBHelper dbHelper;
    private int flag=0;
    public NewsDao(Context context) {
        dbHelper=new DBHelper(context,DBHelper.DATABASE_NAME,null,DBHelper.DATABASE_VERSION);
    }

    public void insertNews(NewsBean news){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
/*        if(flag==0){
            flag=1;
            db.execSQL("DROP TABLE IF EXISTS news");
            dbHelper.onCreate(db);
        }*/
            ContentValues values=new ContentValues();
            values.put("title",news.getTitle());
            values.put("time",news.getTime());
            values.put("url",news.getUrl());
            values.put("imageUrl",news.getImageUrl());
            values.put("flag",news.getFlag());
            values.put("type",news.getType());
            db.insert("news",null,values);

    }

    public ArrayList<NewsBean> queryNews(String type){
        ArrayList<NewsBean> newsBeans=new ArrayList<NewsBean>();
        SQLiteDatabase db=dbHelper.getReadableDatabase();
        Cursor cursor=db.query("news",null,"type=?",new String[]{type},null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                NewsBean newsBean=new NewsBean();
                newsBean.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
                newsBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                newsBean.setType(cursor.getString(cursor.getColumnIndex("type")));
                newsBean.setTime(cursor.getString(cursor.getColumnIndex("time"))); ;
                newsBean.setUrl(cursor.getString(cursor.getColumnIndex("url")));
                newsBean.setImageUrl(cursor.getString(cursor.getColumnIndex("imageUrl")));
                newsBeans.add(newsBean);
            } while (cursor.moveToNext());
        }
        return newsBeans;
    }
    public void update_Flag(NewsBean newsBean){
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        String url=newsBean.getUrl();
        values.put("flag",newsBean.getFlag());
        db.update("news",values,"url=?",new String[]{url});
    }
    public ArrayList<NewsBean> Search(String str){
        ArrayList<NewsBean> newsBeans=new ArrayList<NewsBean>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("news", null, "title like '%"+str+"%' or type like '%"+str+"%'", null, null, null, null, null);
        if(cursor.moveToFirst()){
            do{
                NewsBean newsBean=new NewsBean();
                newsBean.setFlag(cursor.getInt(cursor.getColumnIndex("flag")));
                newsBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                newsBean.setType(cursor.getString(cursor.getColumnIndex("type")));
                newsBean.setTime(cursor.getString(cursor.getColumnIndex("time"))); ;
                newsBean.setUrl(cursor.getString(cursor.getColumnIndex("url")));
                newsBean.setImageUrl(cursor.getString(cursor.getColumnIndex("imageUrl")));
                newsBeans.add(newsBean);
            } while (cursor.moveToNext());
        }
        return newsBeans;
    }
}

